package Groep1;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Toepasingstaak_3_1 {
	
	//Wat variablen
	static ArrayList<String> downloads = new ArrayList<String>();
	static URL mainURL;
	static String htmlcontent; 
	
	/**
	 * Download method, voor het downloaden van een URL naar een bestand. 
	 * 
	 * @param url
	 * @param localFileName
	 * @throws IOException 
	 */
	public static void download(URL url, String localFileName) throws IOException {
		
		//Wat streams/connection variabelen
		URLConnection conn = null;
		BufferedReader  in = null;
		String content = null;
		htmlcontent = null;
		
		
		//Connectie met URL maken
		conn = url.openConnection();

		System.out.println("URL: " + conn.getURL());
		
		StringBuffer stringbuffer = new StringBuffer();
		
		try{
			in = new BufferedReader(new InputStreamReader( conn.getInputStream()));
		while((content = in.readLine())!= null){
			stringbuffer.append(content);	
		}
		htmlcontent = stringbuffer.toString();
		
		in.close();
		//System.out.println(urls);
		}catch(FileNotFoundException e){
			System.err.println(url+ " bestaat niet");
		}
		
		try {
		FileWriter fw = new FileWriter(new File(localFileName));
		fw.write(stringbuffer.toString());
		fw.flush();
		fw.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * getLinks method, zoekt naar links in een URL
	 * 
	 * @param uriStr
	 * @return ArrayList<String>
	 * 
	 */
    public static ArrayList<String> getLinks(URL uriStr) throws URISyntaxException {
    	//Lege ArrayList aanmaken
    	ArrayList<String> result = new ArrayList<String>();
    	
    	//Hier regex zoeken naar urls
		String content = htmlcontent;
		String newURI = null;

		Pattern pattern = Pattern.compile("(src|href)=" +
				"('|\")" +
				"(([a-z0-9A-Z]|/|_|-|\\.)+)" +
				"('|\"|<)" );
		
		// finds a viewstate value in the string with regular expression
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) { 
			//System.out.println(matcher);
			try{
				if(matcher.group(3).toString().startsWith("/")){
					newURI = new URI(mainURL.toString()).resolve(new URI(matcher.group(3))).toString();
				}else{
					newURI = new URI(mainURL.toString()).resolve(new URI("/" + matcher.group(3))).toString();
				}
				
				try{
					if(!result.contains(uriStr.toString()+ "/"+ newURI)){
						result.add(newURI);		
					}
				}catch(IllegalArgumentException e){
					e.getCause();
					System.err.println(newURI + " kan niet worden gedownload");
				}
			}finally{
				//search(new URL("http://"+url+ "/"+ newURI));				
			}
		}
  
        //Alle gevonden links teruggeven       
        return result;
    }

    /**
     * Method loopLinks, gaat alle links van een pagina downloaden
     * 
     * @param url
     * @param dir
     * @param count
     */
    public static void loopLinks(URL url, File dir, int count){
    	try {
    		//Links op pagina ophalen
    		ArrayList<String> links = getLinks(url);
    		
    		//Door gevonden urls lopen
			for (String s : links) {
						
				//URL valideren
				URL tURL = null;
				if(s.contains(mainURL.toString()) || s.contains("http")){
					tURL = new URL(s);
				}else{
					tURL = new URL(mainURL.toString() + s);
				}
			
				
				//URL omzetten naar file
				File file = new File(tURL.getFile());
				
				//Filenaam bepalen
				String nfilename;	
				if(file.getName().matches("[^.]+?\\.(\\w*)")){
					nfilename = file.getName();
				}else{
					nfilename = file.getName() + ".html";
				}
				
					//Controle of FILE niet al X gedownload is
					File checkFile = new File(dir + "\\" + nfilename);
					if(!checkFile.exists() && !nfilename.contains("?")) {
						System.out.println("Download: " + tURL.toString() );
						download(tURL, dir + "\\" + nfilename);
						
					}
			}
			
			//Ga verder met de URL
			System.out.println(count);
			startGetting(url, dir, count);
    	}
    	catch(Exception ex){
    		System.out.println("loopLinks");
    		ex.printStackTrace();
    		return;
    	}
    }
    
    /**
     * Method startGetting, haalt alle links * diep op
     * 
     * @param url
     * @param dir
     * @param count
     */
    public static void startGetting(URL url, File dir, int count){
    	try {
    	
    		//Links zoeken in URL
	    	ArrayList<String> links = getLinks(url);
	    	
	    	//Als diepte groter is dan 0 in de URL nieuwe links zoeken
	    	if(count > 0){
				
	    		//Diepte verlagen met 1
				count = count -1;
				
				//Door gevonden links loopen
				for (String s : links) {
					
					//URL valideren
					URL tURL = null;
					if(s.contains(mainURL.toString()) || s.contains("http"))
						tURL = new URL(s);
					else
						tURL = new URL(mainURL.toString() + s);
					
					//Links downloaden van site
					loopLinks(tURL, dir, count);
				}
	    	}
    	}catch(Exception ex) {
    		System.out.println("startGetting");
    		ex.printStackTrace();
    	}
    }
    
    /**
     * Method main
     * 
     * @param args
     */
	public static void main(String[] args) {		
		
		try {
			//URL opvragen van user
			Scanner scan = new Scanner(System.in);		
			System.out.println("Type location to download:");
			String input = scan.nextLine();
			scan.close();
			
			//URL valideren
			if(input.contains("http://") || input.contains("https://"))
				mainURL = new URL(input);
			else
				mainURL = new URL("http://" + input);
			
			//Download map voor site aanmaken
			File dir = new File(mainURL.getHost());
			dir.mkdir();
			
			//Filename toekennen aan te downloaden file
			String filename = mainURL.getFile();			
			if(filename.equals(""))
				filename = "index.html";
					
			//Hoofdpagina downloaden/opslaan
			download(mainURL, dir + "\\" + filename);
			
			//Links in URL zoeken en downloaden (3 diep)
			startGetting(mainURL, dir, 3);
						
		} catch (Exception ex) {
			System.out.println("main");
			ex.printStackTrace();
		}
	}

}
